package com.moetube.video.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.moetube.entity.PageResult;
import com.moetube.entity.Result;
import com.moetube.entity.StatusCode;
import com.moetube.util.IdWorker;
import com.moetube.video.pojo.Video;
import com.moetube.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 视频实现层
 * @author ccr
 * @version 1.0
 */
@Controller
@CrossOrigin
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    @ResponseBody
    public Result findAll(){
        List<Video> videoList = videoService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",videoList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public Result findById(@PathVariable String id){
        Video video = videoService.findById(id);
        //['搞笑','动漫','日常']
        String label = video.getLabel();
        ArrayList list = JSON.parseObject(label, ArrayList.class);
        video.setList(list);

        return new Result(true,StatusCode.OK,"查询成功",video);
    }


    /***
     * 新增数据
     * @param video
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result add(@RequestBody Video video){
        long id = idWorker.nextId();
        video.setId(String.valueOf(id));
        videoService.add(video);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param video
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    @ResponseBody
    public Result update(@RequestBody Video video,@PathVariable String id){
        video.setId(id);
        videoService.update(video);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    @ResponseBody
    public Result delete(@PathVariable String id){
        videoService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    @ResponseBody
    public Result findList(@RequestParam Map searchMap){
        List<Video> list = videoService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    @ResponseBody
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Video> pageList = videoService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }



}